/******************************************************************************
 * Copyright (C) 2016 Wuhan Water Elephant Co.Ltd All Rights Reserved. 
 * 本软件为武汉水象科技有限公司开发研制。 未经本公司正式书面同意，其他任何个人、
 * 团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package com.sxfq.loansupermarket.business.customer.service.impl;

import com.sxfq.loansupermarket.business.base.service.impl.BaseServiceImpl;
import com.sxfq.loansupermarket.business.customer.service.CustomerBaseInfoService;
import com.sxfq.loansupermarket.common.utils.BeanUtil;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.sxfq.loansupermarket.business.customer.entity.CustomerBaseInfo;
import com.sxfq.loansupermarket.business.customer.mapper.CustomerBaseInfoMapper;

/**
 * 
 * 
 * Module:
 * 
 * CustomerBaseInfoServiceImpl.java
 * 
 * @author 程盼
 * @since JDK 1.8
 * @version 1.0
 * @description: <描述>
 */
@Service
public class CustomerBaseInfoServiceImpl extends BaseServiceImpl<CustomerBaseInfo, Long>
		implements CustomerBaseInfoService {
	@Resource
	private CustomerBaseInfoMapper customerBaseInfoMapper;

	@Override
	public CustomerBaseInfo saveOrUpdateByPhone(CustomerBaseInfo customerBaseInfo) {
		if (customerBaseInfo == null || StringUtils.isEmpty(customerBaseInfo.getPhone())) {
			return null;
		}
		String phone = customerBaseInfo.getPhone();
		CustomerBaseInfo queryCustomerBaseInfo = selectByPhone(phone);
		Date nowDate = new Date();
		customerBaseInfo.setUpdateTime(nowDate);
		if (queryCustomerBaseInfo != null) {
			customerBaseInfo.setId(queryCustomerBaseInfo.getId());
			customerBaseInfo.setBorrowerId(null);
			customerBaseInfo.setCreateTime(null);
			customerBaseInfoMapper.updateByPrimaryKeySelective(customerBaseInfo);
			BeanUtil.copyNotEmptyProperties(queryCustomerBaseInfo, customerBaseInfo);
			return queryCustomerBaseInfo;
		} else {
			customerBaseInfo.setCreateTime(nowDate);
			customerBaseInfoMapper.insertSelective(customerBaseInfo);
			return customerBaseInfo;
		}
	}

	@Override
	public CustomerBaseInfo saveOrUpdate4LoginByBorrowerIdOrPhone(CustomerBaseInfo customerBaseInfo) {
		if (customerBaseInfo == null || StringUtils.isEmpty(customerBaseInfo.getPhone())) {
			return null;
		}
		String phone = customerBaseInfo.getPhone();
		Long borrowerId = customerBaseInfo.getBorrowerId();
		CustomerBaseInfo queryCustomerBaseInfo = selectByPhone(phone);
		if (queryCustomerBaseInfo == null && borrowerId != null) {
			queryCustomerBaseInfo = selectByBorrowerId(borrowerId);
		}
		if (queryCustomerBaseInfo == null) {// 存在只新增
			Date nowDate = new Date();
			customerBaseInfo.setUpdateTime(nowDate);
			customerBaseInfo.setCreateTime(nowDate);
			customerBaseInfoMapper.insertSelective(customerBaseInfo);
			return customerBaseInfo;
		} else {
			String queryPhone = queryCustomerBaseInfo.getPhone();
			if (StringUtils.isEmpty(queryPhone)) {
				queryCustomerBaseInfo.setPhone(phone);
				customerBaseInfoMapper.updateByPrimaryKeySelective(queryCustomerBaseInfo);
			}
			return queryCustomerBaseInfo;
		}
	}

	@Override
	public CustomerBaseInfo selectByBorrowerId(Long borrowerId) {
		if (borrowerId == null || borrowerId <= 0L) {
			return null;
		}
		CustomerBaseInfo param = new CustomerBaseInfo();
		param.setBorrowerId(borrowerId);
		List<CustomerBaseInfo> list = customerBaseInfoMapper.select(param);
		CustomerBaseInfo queryCustomerBaseInfo = null;
		if (list != null && !list.isEmpty()) {
			queryCustomerBaseInfo = list.get(list.size() - 1);
		}
		return queryCustomerBaseInfo;
	}

	@Override
	public CustomerBaseInfo selectByPhone(String phone) {
		if (StringUtils.isEmpty(phone)) {
			return null;
		}
		CustomerBaseInfo param = new CustomerBaseInfo();
		param.setPhone(phone);
		List<CustomerBaseInfo> list = customerBaseInfoMapper.select(param);
		CustomerBaseInfo queryCustomerBaseInfo = null;
		if (list != null && !list.isEmpty()) {
			queryCustomerBaseInfo = list.get(list.size() - 1);
		}
		return queryCustomerBaseInfo;
	}
}